package com.zdzx.controller.admin.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.ApplicationFunctionMapper;
import com.zdzx.mapper.ApplicationMapper;
import com.zdzx.model.service.base.vo.Application;
import com.zdzx.model.service.base.vo.ApplicationFunction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "系统")
@RestController
@RequestMapping("/admin")
public class ApplicationController {

	
	@Autowired
	private ApplicationMapper applicationMapper;
	@Autowired
	private ApplicationFunctionMapper applicationFunctionMapper;
	
	@ApiOperation(value = "获取列表", notes = "") 
	@GetMapping("/application") 
	public Result<PageInfo<Application>> list(
			@ApiParam("") @RequestParam(value = "pageNum", required = false)Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false)Integer pageSize){
		Page<Application> page=new Page<Application>(pageNum, pageSize);
		 IPage<Application> info = applicationMapper.selectPage(page, null);
		 PageInfo<Application> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	
	@ApiOperation(value = "设置系统功能", notes = "")
	@PostMapping("/application/fun")
	public Result<String> saveApplicationFun(@RequestBody List<ApplicationFunction> applicationFunctions){
		if(applicationFunctions!=null&&applicationFunctions.size()>0) {
			QueryWrapper<ApplicationFunction> wrapper=new QueryWrapper<ApplicationFunction>();
			wrapper.eq("applicationId", applicationFunctions.get(0).getApplicationId());
			applicationFunctionMapper.delete(wrapper);
			for (ApplicationFunction applicationFunction : applicationFunctions) {
				applicationFunctionMapper.insert(applicationFunction);
			}
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, null);
		}
		return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	
	
	@ApiOperation(value = "添加", notes = "") 
	@PostMapping("/application") 
	public Result<Integer> save(@RequestBody Application entity){
		int rows = applicationMapper.insert(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "修改", notes = "") 
	@PutMapping("/application") 
	public Result<Integer> edit(@RequestBody Application entity){
		int rows = applicationMapper.updateById(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/application/{id}") 
	public Result<Integer> del(@PathVariable("id")String id){
		int rows = applicationMapper.deleteById(id);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
}
